Home > HOT Posts!, InDesign Scripts > Extract Metadata with Adobe XMP [Part 1]

Extract Metadata with Adobe XMP [Part 1]

Almost every media file contains info about it’s contents. Adobe’s XMP (Extensible Metadata Platform) is, as they say, “a labeling technology that allows you to embed data about a file, known as metadata, into the file itself”. Devices like digital camera’s, camcorders, mobile phones also save metadata, and we are able to retrieve that info later. “Various standards initiatives are using XMP as their framework of choice for implementation”. More info can be found here: Industry standards groups powered by XMP. So, let’s see how to grab all saved data from file with really simple code.

There is two ways to extract data from XMP. First is to direct access data with XMP’s ‘getProperty’, or to ‘serialize();’ data we got from XMP and save it to file. In this example I’ll cover second method. We are going to use ‘AdobeXMPScript’ library. So, we first have to load library, and before loading, we first check is it allready loaded:

//load XMP Library
var XMPload = Boolean(false);
if (ExternalObject.AdobeXMPScript == undefined){
    try {ExternalObject.AdobeXMPScript = new ExternalObject('lib:AdobeXMPScript'); XMPload = true;}
    catch(ex) {alert("Unable to load the AdobeXMPScript library!");}
}

Good, now when we have library loaded, we can retreive complete metadata from selected image frame. This will extract info from file original XML format!

if(XMPload){
    var myFile = File(app.selection[0].graphics[0].itemLink.filePath);
    xmpFile = new XMPFile(myFile.fsName, XMPConst.UNKNOWN, XMPConst.OPEN_FOR_READ);
    xmp = xmpFile.getXMP();
    var myXmp = xmp.serialize();
    xmpFile.closeFile(XMPConst.CLOSE_UPDATE_SAFELY);
}

Great! ‘myXmp’ now holds XML structured metadata extracted directly from file. We can now save it to file. I’m going to use simple javascript file writing.

writeXMP(myXmp, File('/c/myXMP.xml'));

function writeXMP(xmpData, xmpFile){
	xmpFile.open ( 'w', 'Text');
	xmpFile.encoding = 'UTF-8';
	xmpFile.write (xmpData);
	xmpFile.close ();
}

Excellent! We now have complete metadata stored in XML file, ready for reading. It’s little bit tricky to read ‘Array valued XMP properties’… In some of future posts, I’ll try to cover how to read direct from XMP, without saving XML data to file.

Have fun! 😀

  1. Manmohan Mathur
    June 24, 2011 at 05:53

    Unable to load the AdobeXMPScript library!

    Don’t know what’s wrong with me!

    Is there any prerequisite to load the xml library?

    Mac

  1. August 13, 2010 at 16:09
  2. August 17, 2010 at 01:14
  3. August 30, 2010 at 20:27
  4. September 7, 2010 at 01:57
  5. June 1, 2011 at 14:26

Leave a comment